<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-07-28
 * Time: 11:53
 */
//  物流接口
class mobileapi_rpc_express extends mobileapi_frontpage{
    function __construct($app){
        parent::__construct($app);
        
        $this->secret = '64f420a7-432b-4d17-824b-ceb3a686a3ec';
        $this->appkey = '249d3566-f51f-43c4-aa0f-6ddf32e8fb4e';
        $this->format = 'json';
        $this->v = '1.0';
        $this->token = 'c4cad18f-2e8f-467d-b4ec-da511b6949e9';
        $this->url = "http://120.26.115.70:6060/wtdex-service/ws/openapi/rest/route";
        $this->rpcService = kernel::single('base_rpc_service');
    }
//    返回用户对应订单的物流信息
    function order_exp($parms){
        if(!empty($parms['order_id'])){
            $orderid = $parms['order_id'];
        }else{
            return '亲，请传入订单号';
        }
        //生成时间戳，传递参数，组合sign签名,再加密
        $time = date("Y-m-d H:i:s",time());
        $param = array(
            'orderId' => $orderid,
        );
        $parm = json_encode($param);
        $method = 'wtdex.trade.express.get';
        $secret = $this->secret;
        $appkey = $this->appkey;
        $format = $this->format;
        $v = $this->v;
        $token = $this->token;
        $sign = "{$secret}{$appkey}{$format}{$method}{$parm}{$time}{$token}{$v}{$secret}";
        $sign = base64_encode(strtolower(md5($sign)));
//        生成url，post参数
        $url = $this->url;
        $post_arr = array(
            'appkey' => $appkey,
            'sign' => $sign,
            'token' => $token,
            'v' => $v,
            'format' => $format,
            'method' => $method,
            'timestamp' => $time,
            'params' => $parm,
        );
//        用curl函数模拟post请求，最后关闭curl对象
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_arr);
        $data = curl_exec($curl);
        curl_close($curl);
//        对接口返回的$data进行json解码
        $data = json_decode($data);
//        从data内取出数据在组合
        $result['exp']['company'] = $data->response->expressRoute->expressName;
        $result['exp']['number'] = $data->response->expressRoute->expressNo;
        $arr = $data->response->expressRoute->expressRouteItemList->expressRouteItems;
        $state = array(
            '10001' => '收到订单信息',
            '10003' => '进境清关',
            '10007' => '分拣包装',
            '10009' => '保税仓出区',
            '10010' => '国内派送',
            '20010' => '收到订单信息',
            '20020' => '海外仓待入库',
            '20023' => '待拣货',
            '20026' => '拣货中',
            '20030' => '海外仓出库',
            '20040' => '国际运输',
            '20045' => '货已到岸',
            '20050' => '进境清关',
            '20060' => '等待出区',
            '20070' => '国内派送',
            '50' => '已发货',
            '3036' => '在途中',
            '44' => '正在派件',
            '8000' => '已签收',
            '80' => '派送成功',
            '70' => '派件不成功',
            '7070' => '拒收'
        );
        $k = 0;
        for($i = count($arr)-1;$i > 0;$i--){

            $result['msg'][$k]['state'] = $state[$arr[0]->state];
            $result['msg'][$k]['notes'] = $arr[$i]->notes;
            $result['msg'][$k]['optime'] = $arr[$i]->optime;
            $k++;
        }
        return $result;
    }

//    查询订单信息
    public function checkorder($parms){
        if(!empty($parms['order_id'])){
            $orderid = $parms['order_id'];
        }else{
            return '亲，请传入订单号';
        }
        //生成时间戳，传递参数，组合sign签名,再加密
        $time = date("Y-m-d H:i:s",time());
        $param = array(
            'orderId' => $orderid,
        );
        $parm = json_encode($param);
        $method = 'wtdex.trade.order.get';
        $secret = $this->secret;
        $appkey = $this->appkey;
        $format = $this->format;
        $v = $this->v;
        $token = $this->token;
        $sign = "{$secret}{$appkey}{$format}{$method}{$parm}{$time}{$token}{$v}{$secret}";
        $sign = base64_encode(strtolower(md5($sign)));
//        生成url，post参数
        $url = $this->url;
        $post_arr = array(
            'appkey' => $appkey,
            'sign' => $sign,
            'token' => $token,
            'v' => $v,
            'format' => $format,
            'method' => $method,
            'timestamp' => $time,
            'params' => $parm,
        );
//        用curl函数模拟post请求，最后关闭curl对象
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_arr);
        $data = curl_exec($curl);
        curl_close($curl);
//        对接口返回的$data进行json解码
        $data = json_decode($data);
        return $data;
    }
//    删除订单
    public function createOrder(){
        if(!empty($_POST['order_id'])){
            $orderid = $_POST['order_id'];
        }else{
            return '亲，请传入订单号';
        }
        //生成时间戳，传递参数，组合sign签名,再加密
        $time = date("Y-m-d H:i:s",time());
        $param = array(
            'orderId' => $orderid,
        );
        $parm = json_encode($param);
        $method = 'wtdex.trade.order.add';
        $secret = $this->secret;
        $appkey = $this->appkey;
        $format = $this->format;
        $v = $this->v;
        $token = $this->token;
        $sign = "{$secret}{$appkey}{$format}{$method}{$parm}{$time}{$token}{$v}{$secret}";
        $sign = base64_encode(strtolower(md5($sign)));
//        生成url，post参数
        $url = $this->url;
        $post_arr = array(
            'appkey' => $appkey,
            'sign' => $sign,
            'token' => $token,
            'v' => $v,
            'format' => $format,
            'method' => $method,
            'timestamp' => $time,
            'params' => $parm,
        );
//        用curl函数模拟post请求，最后关闭curl对象
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_arr);
        $data = curl_exec($curl);
        curl_close($curl);
//        对接口返回的$data进行json解码
        $data = json_decode($data);
        return $data;
    }

//    取消订单
    public function delorder($parms){
        if(!empty($parms['order_id'])){
            $orderid = $parms['order_id'] ;
        }else{
            return '亲，请传入订单号';
        }
        //生成时间戳，传递参数，组合sign签名,再加密
        $time = date("Y-m-d H:i:s",time());
        $param = array(
            'orderId' => $orderid,
        );
        $parm = json_encode($param);
        $method = 'wtdex.trade.order.delete';
        $secret = $this->secret;
        $appkey = $this->appkey;
        $format = $this->format;
        $v = $this->v;
        $token = $this->token;
        $sign = "{$secret}{$appkey}{$format}{$method}{$parm}{$time}{$token}{$v}{$secret}";
        $sign = base64_encode(strtolower(md5($sign)));
//        生成url，post参数
        $url = $this->url;
        $post_arr = array(
            'appkey' => $appkey,
            'sign' => $sign,
            'token' => $token,
            'v' => $v,
            'format' => $format,
            'method' => $method,
            'timestamp' => $time,
            'params' => $parm,
        );
//        用curl函数模拟post请求，最后关闭curl对象
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_arr);
        $data = curl_exec($curl);
        curl_close($curl);
//        对接口返回的$data进行json解码
        $data = json_decode($data);
        if($data->response->code == 0){
            $this->rpcService->send_user_error('error','取消失败');
        }else{
            $sql = "update ".kernel::database()->prefix."b2c_orders set status = 'dead' where order_id = {$orderid}";
            kernel::database()->exec($sql);
            return '取消成功';
        }
    }
}